package cn.com.selenium.service;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author keying.xia
 * @date 2017/12/6
 */
@Service
public class SeleniumService {

    /**
     * 使用无界面浏览器phantomjs，内置webkit内核
     * phantomjs客户端需要下载，下载地址 http://phantomjs.org/download.html
     */
    public static  void phantomjs() {

        try {

            //设置必要参数
            final  DesiredCapabilities dcaps = new DesiredCapabilities();
            //ssl证书支持
            dcaps.setCapability("acceptSslCerts", true);
            //截屏支持
            dcaps.setCapability("takesScreenshot", true);
            //css搜索支持
            dcaps.setCapability("cssSelectorsEnabled", true);
            //js支持
            dcaps.setJavascriptEnabled(true);
            //驱动支持，在此更换phantomjs安装路径
            //dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "/Users/nealmiao/Documents/Development/virtualenv/phantomjs-2.1.1-macosx/bin/phantomjs");

            dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "D:/phantomjs.exe");

            //创建无界面浏览器对象
            final  PhantomJSDriver driver = new PhantomJSDriver(dcaps);



            // 让浏览器访问空间主页
            String url_str = "http://192.168.100.83:8080/login.html";
            driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
            driver.get(url_str);
            //System.out.println(driver.getPageSource());
            driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
            Thread.sleep(1000L);
            WebElement username = driver.findElement(By.id("username"));
            username.sendKeys("ningbo");
            WebElement passwd = driver.findElement(By.id("password"));
            passwd.sendKeys("admin");
            WebElement pwdLoginbutton = driver.findElement(By.className("login-form-submit"));
            pwdLoginbutton.click();

            Thread.sleep(30000L);
            driver.get("http://192.168.100.83:8080/index.html#dataWall");
            Thread.sleep(15000L);
            WebElement element = driver.findElement(By.id("dataWallApp"));
            String result = driver.getPageSource();
            System.out.println(result);
            Pattern pattern = Pattern.compile("<svg .*?></svg>");
            Matcher matcher = pattern.matcher(result);
            List<String> ls = new ArrayList<String>();
            while (matcher.find()) {
                ls.add(matcher.group());
            }

            System.out.println(ls.size());

            int length = ls.size();

            if (length >= 3) {
                length = 3;
            }

            for (int i = 0; i < length; i++) {
                System.out.println(ls.get(i));
            }


        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
